﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Threading;


namespace CoreClasses
{
    [Serializable]
    public class SerializableFrameworkElement:ISerializable
    {
        private ContentControl _element;

        public SerializableFrameworkElement()
        {
            Element = new ContentControl();
        }
        public SerializableFrameworkElement(ContentControl element)
        {
            Element = element;
        }

        public SerializableFrameworkElement(SerializationInfo info, StreamingContext context)
        {
            

            var m = (MemoryStream)info.GetValue("UserControl", typeof(MemoryStream));
            
            m.Position = 0;
            Element =  XamlReader.Load(m) as ContentControl;
        }
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            Stream m = new MemoryStream();
            XamlWriter.Save(Element, m);
            m.Position = 0;
            
            info.AddValue("UserControl", m);
            
        }

        public Data GetContractData()
        {
            Stream m = new MemoryStream();
            XamlWriter.Save(Element, m);
            m.Position = 0;
            var bb = new BinaryReader(m);
            return new Data() {Binary = bb.ReadBytes((int)m.Length)};

        }

        public ContentControl Element
        {
            get { return _element; }
            set
            {
                _element = value;
                
            }
        }

        [Serializable]
        public class Data
        {
            private byte[] _data;

            public byte[] Binary
            {
                get { return _data; }
                set { _data = value; }
            }

            public SerializableFrameworkElement GetObject()
            {
                var m = new MemoryStream(_data);
                m.Position = 0;
                return new SerializableFrameworkElement(XamlReader.Load(m) as ContentControl);
            }
        }
    }
    public static class ExtensionMethods
    {
        public static async Task<T> XamlClone<T>(this T original)
            where T : class
        {
            if (original == null)
                return null;
            var res = await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
            {
                object clone = null;
                using (var stream = new MemoryStream())
                {
                    XamlWriter.Save(original, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    clone = XamlReader.Load(stream);
                }

                if (clone is T)
                    return (T)clone;
                else
                    return null;
            }, DispatcherPriority.ContextIdle);
            return res;
        }

        public static T XamlSyncClone<T>(this T original)
            where T : class
        {
            if (original == null)
                return null;

            object clone = null;
            using (var stream = new MemoryStream())
            {
                XamlWriter.Save(original, stream);
                stream.Seek(0, SeekOrigin.Begin);
                clone = XamlReader.Load(stream);
            }

            if (clone is T)
                return (T)clone;
            else
                return null;
        }
    }
}
